home *** CD-ROM | disk | FTP | other *** search
- Path: lantana.singnet.com.sg!usenet
- From: Teddy Bear <s7700038@singnet.com.sg>
- Newsgroups: comp.lang.c
- Subject: Re: Almost got it
- Date: 12 Mar 1996 10:26:38 GMT
- Organization: Singapore Telecom Internet Service
- Message-ID: <4i3jgu$tfn@lantana.singnet.com.sg>
- References: <4i1ebo$96a@lantana.singnet.com.sg>
- NNTP-Posting-Host: ts900-4115.singnet.com.sg
- Mime-Version: 1.0
- Content-Type: multipart/mixed;
- boundary="-------------------------------1296297876771"
- X-Mailer: Mozilla 1.22 (Windows; I; 16bit)
-
- This is a multi-part message in MIME format.
-
- ---------------------------------1296297876771
- Content-Transfer-Encoding: 7bit
- Content-Type: text/plain; charset=us-ascii
-
- I can't save 2 or more list of particulars for my program. When i
- load it back from disk, it returns me rubbish Please help me check it
- I need it tommorow
- Thanks in advance
-
- ---------------------------------1296297876771
- Content-Transfer-Encoding: 7bit
- Content-Type: text/plain
-
- #include <stdio.h>
- #include <conio.h>
- #include <string.h>
- #include <stdlib.h>
-
-
- typedef struct node{
- int age; //individual's age
- char name[40]; //individual's name
- char address[40]; //individual's address
- struct node *next;
- }node;
- node *head;
- typedef node* nodeptr;
-
- void createlist(nodeptr *ptrtohead, nodeptr head);
- int countlist(nodeptr head);
- nodeptr searchlist(nodeptr head, char *wanted);
- void editlist(nodeptr head);
- void displaylist(nodeptr head);
- void save_record(FILE *fileptr, nodeptr *head, int count);
- void load_record(FILE *fileptr, nodeptr *head, nodeptr *tail, int *count);
- nodeptr addlist(nodeptr head);
- nodeptr deletelist(nodeptr head);
- int main()
- {
- char option;
- nodeptr ptr, *ptrtohead, head, tail;
- int count=0;
- FILE *fileptr;
- head = NULL;
-
- option = '1';
- do
- {
- clrscr();
- printf("\n\n\nThis program .....");
- printf("\n\n1) Read existing list from disk\n");
- printf("2) Add list\n");
- printf("3) Delete list\n");
- printf("4) Search list\n");
- printf("5) Display List\n");
- printf("6) Save list to disk\n");
- printf("7) Kill Yi Yong !!! ( EXIT )\n");
- printf("\nPlease enter your choice : ");
-
- if(option < '1' || option > '7')
- printf("\nInvaild option. Please re-enter:");
- option = getche();
- switch(option)
- {
- case '1' : load_record(fileptr, &head, &tail, &count);
- displaylist(head);
- printf("\n\nPress any key to continue.. ");
- getch();
- break;
- case '2' : if(head == 0)
- {
- createlist(ptrtohead, NULL);
- head = *ptrtohead;
- }
- else
- head = addlist(head);
- printf("\n\nPress any key to continue.. ");
- getch();
- break;
- case '3' : if (head == 0)
- printf("nothing to delete");
- head = deletelist(head);
- printf("\n\nPress any key to continue.. ");
- getch();
- break;
- case '4' : editlist(head);
- printf("\n\nPress any key to continue.. ");
- getch();
- break;
- case '5' : displaylist(head);
- printf("\nPress any key to cont...");
- getch();
- break;
- case '6' : save_record(fileptr, &head, count);
- displaylist(head);
- break;
- case '7' : clrscr();
- exit(0);
-
- }
- }
- while(option != '7');
- return 0;
- }
-
- void load_record(FILE *fileptr, nodeptr *head, nodeptr *tail, int *count)
- //get list.dat from disk
- {
- nodeptr load;
- int node_size=sizeof(node);
- int name_length,address_length;
-
- if((fileptr=fopen("b:list.dat","rb"))==NULL)
- {
- printf("\a\n\nUnable to open file\n");
- printf("\nPress any key to continue....");
- getch();
- }
- else
- {
- fscanf(fileptr,"%d\n", &(*count));
- while(!feof(fileptr))
- {
- load=(nodeptr)malloc(node_size);
- fscanf(fileptr, "%d\n", &name_length);
- fscanf(fileptr, "%d\n", &address_length);
- fgets(load->name,name_length+1, fileptr);
- fscanf(fileptr,"\n");
- fgets(load->address, address_length+1, fileptr);
- fscanf(fileptr, "%d\n", &load->age);
-
- if((*head)==NULL)
- {
- (*head)=load;
- (*head)=load;
- }
- else
- {
- (*tail)->next=load;
- (*tail)=load;
- }
- }
- (*tail)->next=NULL;
- }
- fclose(fileptr);
- return;
- }
-
-
- void save_record(FILE *fileptr, nodeptr *head, int count)
- //save info to disk file list.dat
- {
- nodeptr save;
-
- if ((fileptr=fopen("b:list.dat","wb"))==NULL)
- {
- printf("\a\n\nERROR!!!!Unable to open file!!!\n");
- }
- else
- {
- save=(*head);
- fprintf(fileptr,"%d\n",count);
- while(save!=NULL)
- {
- fprintf( fileptr,"%d\n", strlen(save->name));
- fprintf( fileptr, "%d\n", strlen(save->address));
- fprintf( fileptr, "%s\n", save->name);
- fprintf( fileptr, "%s\n", save->address);
- fprintf( fileptr, "%d\n", save->age);
- save=save->next;
- }
- }
- fclose(fileptr);
- printf("\a\n\nSaving Done!!!!!\n");
- printf("\nPress any key to continue.....");
- return;
- }
-
-
-
- void createlist(nodeptr *ptrtohead, nodeptr head)
- //create a link list
- {
- nodeptr temp, last;
- char ans;
- int size = sizeof(node);
- do
- {
- temp=(nodeptr)malloc(size);
- clrscr();
- printf("\nEnter Name : ");
- gets(temp->name);
- printf("Enter Age : ");
- scanf("%d",&temp->age);
- fflush(stdin);
- printf("Enter Address : ");
- gets(temp->address);
-
- if(head == NULL)
- {
- head = temp;
- last = temp;
- }
- else
- {
- last->next=temp;
- last = temp;
- }
- printf("\nWould u like to enter more ? <Y/N> : ");
- ans=toupper(getch());
- }
- while(ans != 'N');
- last->next = NULL;
- *ptrtohead = head;
- }
-
- nodeptr searchlist(nodeptr head, char *want)
-
- {
- int element, count, found, condition;
- nodeptr ptr;
- element = countlist(head);
- ptr = head;
- found = 0;
- condition = 1;
- for(count=0; (count<element)&&(condition); )
- {
- if((strcmp(ptr->name, want))==0)
- {
- found = 1;
- condition = 0;
- }
- else
- if((strcmp(ptr->name, want)) > 0)
- condition = 0;
- else
- {
- ptr = ptr->next;
- count++;
- }
- }
-
- if(found)
- return ptr;
- else
- return NULL;
- }
-
- int countlist(nodeptr head)
- //count no of individuals
- {
- int count;
- nodeptr ptr;
- ptr = head;
- if(ptr != NULL)
- {
- for(count=1;ptr->next!=NULL; count++)
- ptr = ptr->next;
- return count;
- }
- else
- {
- return 0;
- }
- }
-
- void editlist(nodeptr head)
- {
-
- char *find, answer='Y';
- int cnt, check;
- nodeptr ptrtoelm;
- do
- {
- clrscr();
- printf("\n\nEnter Name : ");
- gets(find);
- ptrtoelm = searchlist(head, find);
- if(ptrtoelm==NULL)
- {
- printf("\a\nSORRY! THERE IS NO SUCH NAME\n");
- printf("\nDo you wish to continue searching? <Y/N> : ");
- answer = toupper(getch());
- }
- if(answer == 'N')
- return;
- }while(ptrtoelm==NULL && answer == 'Y');
-
-
- printf("\nName : %s\n", ptrtoelm->name);
- printf("Age : %d\n", ptrtoelm->age);
- printf("Address : %s\n", ptrtoelm->address);
- }
-
-
- void displaylist(nodeptr head)
- // Display the information in the link.
- {
- nodeptr ptr;
- ptr = head;
- clrscr();
- while(ptr!=NULL)
- {
- printf("\nNAME: %s \n",ptr->name);
- printf("AGE: %d \n",ptr->age);
- printf("ADDRESS: %s \n",ptr->address);
- ptr = ptr->next;
- }
-
- }
-
- nodeptr clearlist(nodeptr head)
-
- {
- nodeptr ptr;
- while(head != NULL)
- {
- ptr = head;
- head = head->next;
- free(ptr);
- }
- free(ptr);
- return head;
- }
-
- nodeptr addlist(nodeptr head)
-
- {
-
- nodeptr front, back, newitem;
- int check, cnt;
- newitem = (nodeptr)malloc(sizeof(node));
- clrscr();
- printf("\n\nEnter name to be added : ");
- gets(newitem->name);
- if((strcmp((back->name),(front->name))) == 0)
- {
- printf("\a");
- printf("\n\nDUPLICATE COPY");
- strcpy(back->name,front->name);
- back->age = front->age;
- strcpy(back->address,front->address);
- getch();
- }
- fflush(stdin);
- printf("Enter age : ");
- scanf("%d", &newitem->age);
- fflush(stdin);
- printf("Enter address : ");
- gets(newitem->address);
- if((strcmp(newitem->name, head->name)) <= 0)
- {
- newitem->next = head;
- head = newitem;
- }
- else
- {
- back = head;
- front = back->next;
- while(front!=NULL)
- {
- if((strcmp(newitem->name, front->name)) > 0)
- {
- back = front;
- front = front->next;
- }
- else
- break;
- }
- newitem->next = front;
- back->next = newitem;
- }
- return head;
- }
-
- nodeptr deletelist(nodeptr head)
-
- {
- nodeptr front, back;
- char *tempname;
- if(head != NULL)
- {
- back = head;
- front = back->next;
- }
- else
- {
- printf("\n\aDelete not allowed.");
- getch();
- return NULL;
- }
- tempname = (char *)malloc(sizeof(char *));
- clrscr();
- printf("\n\nEnter name to be delete : ");
- gets(tempname);
- if((strcmp(tempname,back->name)) == 0)
- {
- front = head;
- back = back->next;
- free(front);
- return back;
- }
- else
- {
- while(((strcmp(tempname, front->name))!=0) && (front!=NULL))
- {
- back = front;
- front = front->next;
- }
- if(front==NULL)
- {
- printf("\n\aNo such person in the list.");
- return head;
- }
- else
- {
- back->next = front->next;
- free(front);
- return head;
- }
- }
-
- }
-
-
- ---------------------------------1296297876771--
-